<template>
  <div>
    <UploadExcel :on-success="handleSuccess" />
  </div>
</template>
<script>
import { importEmployee } from '@/api/employees'
import { formatExcelDate } from '@/utils'
export default {
  name: '',
  data() {
    return {
    }
  },
  computed: {
  },
  created() {
  },
  methods: {
    transData(results) {
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      return results.map((item) => {
        var newObj = {}
        var zhKeys = Object.keys(item)
        zhKeys.forEach((key) => {
          if (mapInfo[key] === 'timeOfEntry' || mapInfo[key] === 'correctionTime') {
            newObj[mapInfo[key]] = new Date(formatExcelDate.item[key])
          } else {
            newObj[mapInfo[key]] = item[key]
          }
        })
        return newObj
      })
    },
    async handleSuccess({ results }) {
    //   console.log(results, header, '0000000')
    //   const mapInfo = {
    //     '入职日期': 'timeOfEntry',
    //     '手机号': 'mobile',
    //     '姓名': 'username',
    //     '转正日期': 'correctionTime',
    //     '工号': 'workNumber',
    //     '部门': 'departmentName',
    //     '聘用形式': 'formOfEmployment'
    //   }
    //   var newArray = results.map((item) => {
    //     var newObj = {}
    //     var zhKeys = Object.keys(item)
    //     zhKeys.forEach((key) => {
    //       newObj[mapInfo[key]] = item[key]
    //     })
    //     return newObj
    //   })
      var newResults = this.transData(results)
      //   console.log(newResults, 99999)
      const res = await importEmployee(newResults)
      console.log(res, 8888)
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
